package com.mariusz.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.jdesktop.swingx.JXTitledPanel;

import com.mariusz.data.dto.Property;
import com.mariusz.managers.DataManager;
import com.mariusz.managers.NavigationManager;
import com.mariusz.managers.NavigationManager.Screen;
import com.mariusz.ui.models.PropertiesListModel;
import com.mariusz.utils.Formats;
import com.mariusz.data.enums.PaymentsIntervals;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JTabbedPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXTitledSeparator;
import java.awt.GridBagLayout;

public class LandlordsListPanel extends JXTitledPanel implements
		ActionListener, ListSelectionListener {
	private JTable paymentsTabel;
	private JButton btnBack;
	private JButton btnPopulate;
	private JList listProperties;
	private JTabbedPane landlordsTabbedPane;
	private JPanel paymentsTab;
	private JPanel detailsTab;
	private JPanel reportsTab;
	private JTextField txtLandlordName;
	private JFormattedTextField txtLandlordDeposit;
	private JFormattedTextField txtAmount;
	private JLabel lblLandlordName;
	private JComboBox cmbIntervals;
	private JTextField txtLandlordAddress;
	private JLabel lblLandlordAddress;
	private JTextField txtPhone;
	private JLabel lblPhone;
	private JTextField txtAccountNo;
	private JLabel lblAccountNo;
	private JXLabel lblBeginDate;
	private JXDatePicker dpBeginDate; 
	private JXDatePicker dpEndDate;
	private JButton btnSave;
	
	public LandlordsListPanel() {
		setTitle("Landlords payments");
		getContentContainer().setLayout(null);

		landlordsTabbedPane = new JTabbedPane(JTabbedPane.TOP);
		landlordsTabbedPane.setBounds(198, 0, 486, 435);
		getContentContainer().add(landlordsTabbedPane);

		paymentsTab = new JPanel();
		landlordsTabbedPane.addTab("Landlord's Payments", null, paymentsTab, null);
		paymentsTab.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 181, 385);
		paymentsTab.add(scrollPane);

		paymentsTabel = new JTable();
		paymentsTabel.setBorder(null);
		paymentsTabel.setRowSelectionAllowed(false);
		scrollPane.setViewportView(paymentsTabel);

		detailsTab = new JPanel();
		landlordsTabbedPane.addTab("Landlord's Details", null, detailsTab, null);
		detailsTab.setLayout(null);

		btnPopulate = new JButton("Populate Payments");
		btnPopulate.setBounds(313, 373, 158, 23);
		detailsTab.add(btnPopulate);

		txtLandlordName = new JTextField();
		txtLandlordName.setBounds(10, 21, 234, 20);
		detailsTab.add(txtLandlordName);
		txtLandlordName.setColumns(10);
		
		txtLandlordDeposit = new JFormattedTextField(Formats.CASH);
		txtLandlordDeposit.setBounds(369, 255, 86, 20);
		detailsTab.add(txtLandlordDeposit);
		txtLandlordDeposit.setColumns(10);
		
		JLabel lblDeposit = new JLabel("Deposit:");
		lblDeposit.setBounds(313, 258, 46, 14);
		detailsTab.add(lblDeposit);
		
		JLabel lblPayments = new JLabel("Payment amount:");
		lblPayments.setBounds(271, 291, 86, 14);
		detailsTab.add(lblPayments);
		
		txtAmount = new JFormattedTextField(Formats.CASH);
		txtAmount.setColumns(10);
		txtAmount.setBounds(369, 286, 86, 20);
		detailsTab.add(txtAmount);
		
		cmbIntervals = new JComboBox();
		cmbIntervals.setModel(new DefaultComboBoxModel(PaymentsIntervals.values()));
		cmbIntervals.setBounds(369, 317, 99, 20);
		detailsTab.add(cmbIntervals);
		
		JLabel lblIntervals = new JLabel("Intervals:");
		lblIntervals.setBounds(297, 320, 58, 14);
		detailsTab.add(lblIntervals);
		
		lblBeginDate = new JXLabel();
		lblBeginDate.setText("Begin date:");
		lblBeginDate.setBounds(9, 256, 86, 18);
		detailsTab.add(lblBeginDate);
		
		dpBeginDate = new JXDatePicker();
		dpBeginDate.setBounds(105, 254, 139, 21);
		detailsTab.add(dpBeginDate);
		
		dpEndDate = new JXDatePicker();
		dpEndDate.setBounds(105, 287, 139, 21);
		detailsTab.add(dpEndDate);
		
		JXLabel lblEndDate = new JXLabel();
		lblEndDate.setText("End date:");
		lblEndDate.setBounds(10, 289, 76, 14);
		detailsTab.add(lblEndDate);
		
		lblLandlordName = new JLabel("Name:");
		lblLandlordName.setBounds(10, 7, 103, 14);
		detailsTab.add(lblLandlordName);
		
		JXTitledSeparator titledSeparator = new JXTitledSeparator();
		GridBagLayout gridBagLayout = (GridBagLayout) titledSeparator.getLayout();
		gridBagLayout.rowWeights = new double[]{0.0};
		gridBagLayout.rowHeights = new int[]{0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0};
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		titledSeparator.setTitle("Contract details");
		titledSeparator.setBounds(10, 214, 465, 30);
		detailsTab.add(titledSeparator);
		
		btnSave = new JButton("Save Details");
		btnSave.setBounds(297, 180, 158, 23);
		detailsTab.add(btnSave);
		
		txtLandlordAddress = new JTextField();
		txtLandlordAddress.setColumns(10);
		txtLandlordAddress.setBounds(10, 59, 234, 20);
		detailsTab.add(txtLandlordAddress);
		
		lblLandlordAddress = new JLabel("Address:");
		lblLandlordAddress.setBounds(10, 45, 103, 14);
		detailsTab.add(lblLandlordAddress);
		
		txtPhone = new JTextField();
		txtPhone.setColumns(10);
		txtPhone.setBounds(254, 21, 217, 20);
		detailsTab.add(txtPhone);
		
		lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(254, 7, 217, 14);
		detailsTab.add(lblPhone);
		
		txtAccountNo = new JTextField();
		txtAccountNo.setColumns(10);
		txtAccountNo.setBounds(254, 59, 217, 20);
		detailsTab.add(txtAccountNo);
		
		lblAccountNo = new JLabel("Account No.:");
		lblAccountNo.setBounds(254, 45, 217, 14);
		detailsTab.add(lblAccountNo);
		btnPopulate.addActionListener(this);

		reportsTab = new JPanel();
		landlordsTabbedPane.addTab("Payment's Reports", null, reportsTab, null);

		listProperties = new JList();
		listProperties.setBounds(10, 5, 178, 385);
		getContentContainer().add(listProperties);
		listProperties.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listProperties.addListSelectionListener(this);

		btnBack = new JButton("Back");
		btnBack.setBounds(91, 405, 97, 30);
		getContentContainer().add(btnBack);
		btnBack.addActionListener(this);

	}

	@Override
	public void actionPerformed(ActionEvent s) {
		if (s.getSource() == btnBack) {
			NavigationManager.getInstance().switchTo(Screen.STATUS_PANEL);

		}

	}

	@Override
	public void show(boolean arg0) {
		List<Property> properties = DataManager.getInstance().getProperties();
		listProperties.setModel(new PropertiesListModel(properties));
		DataManager.getInstance().setCurrentProperty(null);
		landlordsTabbedPane.show(false);
		super.show(arg0);
	}

	@Override
	public void valueChanged(ListSelectionEvent event) {
		if (event.getSource() == listProperties) {
			if (!event.getValueIsAdjusting()) {
				int index = listProperties.getSelectedIndex();
				if (index >= 0) {
					Property property = (Property) listProperties.getModel().getElementAt(index);
					DataManager.getInstance().setCurrentProperty(property);
					landlordsTabbedPane.show(true);
				}
			}

		}

	}
}
